import { Util } from '../utils';

class LFTLayerGroup {
  constructor(id, viewer) {
    this.id = id || Util.uuid();
    this.viewer = viewer;
    this._layers = {};
  }

  get id() {
    return this._id;
  }
  set id(id) {
    this._id = id;
  }

  get viewer() {
    return this._viewer;
  }

  set viewer(viewer) {
    this._viewer = viewer;
  }

  getLayer(id) {
    return this._layers[id] || null;
  }

  getLayers() {
    return this._layers;
  }

  addLayer(layer) {
    if (!Object(this._layers).hasOwnProperty(layer.id)) {
      this._layers[layer.id] = layer;
      layer.parent = this;
      layer.groupId = this.id;
    }
    return this;
  }

  removeLayer(id) {
    if (Object(this._layers).hasOwnProperty(id)) {
      this._layers[id].remove();
    }
  }

  clear() {
    Object.keys(this._layers).forEach((id) => {
      this._layers[id].remove();
    });
  }

  remove() {
    this.clear();
    this._viewer && this._viewer.removeLFTLayerGroupInCache(this.id);
  }
}

export default LFTLayerGroup;
